package com.wink_172.library

import android.app.Activity
import android.content.Context
import java.util.*

/**程序生命周期管理 开始到结束*/
class AppManager private constructor() {
    companion object {
        var allActivity: Stack<Activity?>? = null
            //              get() = Companion.appManager
            private set
        private var instance: AppManager? = null

        /**
         * 单一实例
         */
        val appManager: AppManager?
            get() {
                if (instance == null) {
                    instance = AppManager()
                }
                return instance
            }


        private const val TAG = "TAG"
    }

    /**
     * 添加Activity到堆栈
     */
    fun addActivity(activity: Activity?) {
        if (allActivity == null) {
            allActivity = Stack()
        }
        if (activity != null) {
            allActivity!!.add(activity)
        }

    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    fun currentActivity(): Activity? {
        if (allActivity == null) {
            return null
        }
        if (allActivity!!.empty()) {
            return null
        }

        return allActivity!!.lastElement()
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    fun finishActivity() {
        if (allActivity == null) {
            return
        }
        if (allActivity!!.empty()) {
            return
        }

        val activity = allActivity!!.lastElement()
        finishActivity(activity)
    }

    /**
     * 结束指定的Activity
     */
    fun finishActivity(activity: Activity?) {
        var activity = activity
        if (activity != null) {
            allActivity!!.remove(activity)
            activity.finish()
            activity = null
        }
    }

    /**
     * 结束指定类名的Activity
     */
    fun finishActivity(cls: Class<*>) {
        for (activity in allActivity!!) {
            if (activity!!.javaClass == cls) {
                finishActivity(activity)
            }
        }
    }

    /**
     * 获取指定类名的Activity
     *      val danceActivity =
    AppManager.appManager!!.getActivity(DanceActivity::class.java) as DanceActivity?
     *
     *  val activity = getActivity()
    Log.e(TAG, "hello: ====>>00"+activity!!.javaClass.toString() )
    if (activity!!.javaClass == MainActivity::class.java) {}
     * */
    fun getActivity(cls: Class<*>): Activity? {

        if (instance == null) {
            return null
        }
        if (allActivity == null) {
            return null
        }
        if (allActivity!!.empty()) {
            return null
        }
        for (activity in allActivity!!) {
            if (activity != null) {
                if (activity.javaClass == cls) {
                    return activity
                }
            }
        }
        return null
    }

    /**
     * 结束除了ActivityA之外的其他Activity
     */
    fun finishAllActivityExceptA(cls: Class<*>) {
        var temp: Activity? = null
        var i = 0
        val size = allActivity!!.size
        while (i < size) {
            val aa = allActivity!![i]
            if (null != aa) {
                if (aa.javaClass != cls) {
                    aa.finish()
                } else {
                    temp = aa
                }
            }
            i++
        }
        allActivity!!.clear()
        if (temp != null) {
            allActivity!!.add(temp)
        }
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        var i = 0
        val size = allActivity!!.size
        while (i < size) {
            if (null != allActivity!![i]) {
                allActivity!![i]!!.finish()
            }
            i++
        }
        allActivity!!.clear()
    }

    /**
     * 退出应用程序
     */
    fun AppExit(context: Context?) {
        try {
            finishAllActivity()
            System.exit(0)
        } catch (e: Exception) {
        }
    }


}